import Vue from 'vue';
var mpheader = require("../components/mpheader.js");
var dt = require("../components/dateTime.js");
var wl = require("../components/winLocation.js");
var applyContractVue = require("./applyContractVue.js");
var openid, serialNo;
var detailsVue, contractVue;

var recordData = [];
var rderInfoData = {};
var commonData = {
    step: "8",
    showDialog: "0"
};
$(document).ready(function() {
    initPage();
});
var orderStateMap = {
    "01": "待交易",
    "02": "申请成功,已划付",
    "03": "申请成功,已划付",
    "04": "已退货",
    "05": "划付失败,已撤销",
    "99": "分期失败"
};


// 
function craeteDetailVue() {
    var mheder1 = mpheader.getMPheader({
        "titleText": "信用贷款查询"
    }, function() {

    });

    detailsVue = new Vue({
        el: "#details_box",
        data: {
            rateArray: recordData,
            rderInfo: rderInfoData,
            commonData: commonData

        },
        computed: {

        },
        beforeMount: function() {
            console.log("amountslect beforeMount 渲染前");
        },
        mounted: function() {
            console.log("amountslect mounted 加载完");
            getDetailsData();
        },
        components: {
            "mpheader": mheder1
        },
        methods: {
            "toDetails": function(event) {
                var serialNo = $(event.target).parents(".weui-cells").attr("data-serial");
                console.log(serialNo);
                alert("toDetails");
            },
            "lookContract":function  () {
                commonData.step = 6;
            }
        }
    });
}

// 处理数据
function manipulationData(lfqorderInfo) {
    for (var i = 0, l = lfqorderInfo.length; i < l; i++) {
        lfqorderInfo[i].repayDate = dt.getDateStrFormate1(lfqorderInfo[i].instalmentTimeString, "date");
    }

    detailsVue.rateArray = lfqorderInfo;
}

// 取交易记录
function getDetailsData() {
    var rdParameter = {
        open_id: openid,
        serial_no: serialNo,
        servlet_type: "query_orderByOrderId"
    };
    $.post(LFQCreateOrderServlet, rdParameter, function(rddata) {
        rddata = JSON.parse(rddata);
        console.dir(rddata);
        if (rddata.lft_orderinfo_response && rddata.lft_orderinfo_response.resp_code) {
            var lft_orderinfo_response = rddata.lft_orderinfo_response;
            if (lft_orderinfo_response.lfqRepayNotices_arrays && lft_orderinfo_response.lfqRepayNotices_arrays.lfqRepayNotices) {
                manipulationData(lft_orderinfo_response.lfqRepayNotices_arrays.lfqRepayNotices);
            }

            if (rddata.lft_orderinfo_response.lfqorderInfo_arrays && rddata.lft_orderinfo_response.lfqorderInfo_arrays.lfqorderInfo) {
                var lfqorderInfo = rddata.lft_orderinfo_response.lfqorderInfo_arrays.lfqorderInfo[0];
                rderInfoData = lfqorderInfo;
                rderInfoData.stateMsg = orderStateMap[lfqorderInfo.order_state];
                var appDateStr = "" + lfqorderInfo.apply_date + lfqorderInfo.apply_time;
                rderInfoData.dateFormat = dt.getDateStrFormate1(appDateStr, "second");
                detailsVue.rderInfo = rderInfoData;
                createContrant();
            }
        }



    });
}

// 创建合同
function createContrant() {
    var contractVue = applyContractVue.createapplyContractvue(commonData, function() {
        commonData.step = 8;
    }, rderInfoData);
}

// 设置记录父标签的高度
function setRecordParentHeight() {
    var wih = window.innerHeight;
    var rph = (wih - 44) + "px";
    $("#details_page>.page__bd").css("height", rph);
}


// 初始化变量
function initValue() {
    openid = wl.QueryString.GetValue("openid");
    serialNo = wl.QueryString.GetValue("serialNo");
}

function initPage() {
    wl.checkBrowser();
    wl.QueryString.Initial();
    initValue();
    setRecordParentHeight();

    craeteDetailVue();

}